package org.sl.flvplayer.view {
	import org.sl.flvplayer.controls.Controller;
	import org.sl.flvplayer.controls.PlayerLogic;
	import org.sl.flvplayer.flyweights.FWPlayerCommand;

	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.ui.Mouse;
	import flash.utils.Timer;

	/**
	 * Provides the possibility to play pause the video by clicking on the playerscreen and furthermore adds a linked icon.
	 * 
	 * @package org.sl.flvplayer.view
	 * @author Dan
	 * @version
	 * @see org.sl.flvplayer.view.PlayerScreen
	 * @see org.sl.flvplayer.controls.PlayerLogic
	 * @see org.sl.flvplayer.controls.Controller
	 */ 
	public class PlayerScreenInteraction extends Sprite {

		private static var instance : PlayerScreenInteraction;

		private var playerScreen : PlayerScreen;
		private var screenInteraction : Sprite = new Sprite();
		private var top : Number;
		private var bottom : Number;
		private var left : Number;
		private var right : Number;
		private var centerHeight : Number;
		private var centerWidth : Number;
		private	var timer : Timer = new Timer(3000, 1); 

		/**
		 * 
		 */
		public var icon : Loader = new Loader(); 

		/**
		 * 
		 */
		public var watermark : Loader = new Loader(); 

		/**
		 * PlayerScreenInteraction constructor adds a watermark an icon and a transparent rectangle to the stage</br>
		 * and enables mouseClick interaction for the icon.  
		 * 
		 */
		function PlayerScreenInteraction() {
			super();
			mouseChildren = true;
			playerScreen = PlayerScreen.getInstance();
			addChild(watermark);
			addChild(screenInteraction);
			addChild(icon);
			watermark.contentLoaderInfo.addEventListener(Event.COMPLETE, onWatermarkLoaderComplete);	
			icon.addEventListener(MouseEvent.CLICK, popupclick);
			icon.contentLoaderInfo.addEventListener(Event.COMPLETE, onIconLoaderComplete);
		}

		/**
		 * Ensures that there is only one instance of the class by using the singleton pattern.
		 * 
		 * @return instance of the <code>PlayerScreenInteraction</code> class
		 */
		public static function getInstance() : PlayerScreenInteraction {
			if(!instance) instance = new PlayerScreenInteraction();
			return instance;
		}

		/**
		 * Draws a transparent and clickable rectangle.
		 */
		public function initPlayScreenInteraction() : void {			
			screenInteraction.addEventListener(MouseEvent.CLICK, onMouseClick);
			// alpha is 0 thus transparent but clickable
			screenInteraction.graphics.beginFill(0x000000, 0);
			screenInteraction.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			screenInteraction.graphics.endFill();	
		}

		/**
		 * Sets the timer and the listener for the mousepointer. Triggered only when in fullscrenn mode.
		 */
		public function addMousePointerAction() : void {
			screenInteraction.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimer);
		}

		/**
		 * Removes the mousepointer.
		 */
		public function removeMousePointerAction() : void {
			screenInteraction.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimer);
		}

		/**
		 * Shows the mousepointer and the btnScreenPlay element.
		 * 
		 * @param p_event
		 */
		private function onMove(p_event : MouseEvent) : void { 
			Mouse.show();
			timer.reset();
			timer.start();  
			Controller.getInstance().btnScreenPlay.visible = true;
		}

		/**
		 * Hides the mousepointer and the btnScreenPlay element.
		 * 
		 * @param p_event
		 */
		private function onTimer(p_event : TimerEvent) : void {
			Mouse.hide(); 
			Controller.getInstance().btnScreenPlay.visible = false;
		}

		/**
		 * Fits the screenInteraction to the size of the player.
		 */
		public function resizeScreenInteraction() : void {
			screenInteraction.width = stage.stageWidth;
			screenInteraction.height = stage.stageHeight;
		}

		/**
		 * Loads the icon.
		 */
		public function loadIcon() : void {	
			icon.load(playerScreen.source.linkIcon.iconURL);			
		}	

		/**
		 * Loader complete handler.
		 * 
		 * @param p_event
		 */
		private function onIconLoaderComplete(p_event : Event) : void {						
			posIcon(icon);
		}

		/**
		 * Unloads the icon.
		 */
		public function unloadIcon() : void {	
			icon.unload();
		}	

		/**
		 * Loads the watermark.
		 */
		public function loadWatermark() : void {
			watermark.load(playerScreen.source.watermarkIcon.iconURL);
		}			

		/**
		 * Loader complete handler.
		 */
		private function onWatermarkLoaderComplete(p_event : Event) : void {		
			posIcon(watermark);
		}

		/**
		 * Unloads watermark.
		 */
		public function unloadWatermark() : void {
			if(watermark) watermark.unload();
		}

		
		/**
		 * MouseClick handler which sets the PAUSE command.
		 * 
		 * @param p_event
		 */
		private function onMouseClick(p_event : MouseEvent) : void {
			PlayerLogic.getInstance().command(new FWPlayerCommand(FWPlayerCommand.PAUSE));
		}

		/**
		 * Handles mouseClick on icon and opens a new URL in the browser.
		 * 
		 * @param p_event
		 */
		private function popupclick(p_event : MouseEvent) : void {
			var link : URLRequest = new URLRequest(PlayerScreen.getInstance().source.link);
			navigateToURL(link, "_self"); 
		}

		/**
		 * Positions icon or watermark in dependency of the desired position on player start and after resizing of the player.
		 * 
		 * @param loader icon or watermark loader
		 */
		public function posIcon(loader : Loader) : void {
			var  posSrc : String;
			var dummy : DisplayObject;
			if(icon == loader) {
				posSrc = playerScreen.source.linkIcon.iconPos;
				dummy = icon;
			}
			if(watermark == loader) {		
				posSrc = playerScreen.source.watermarkIcon.iconPos;
				dummy = watermark;
			}
			top = playerScreen.playerDisplay.y;
			bottom = playerScreen.playerDisplay.y + playerScreen.playerDisplay.height - dummy.height ;
			left = playerScreen.playerDisplay.x;
			right = playerScreen.playerDisplay.x + playerScreen.playerDisplay.width - dummy.width;
			centerWidth = (stage.stageWidth / 2) - dummy.width / 2;
			centerHeight = (stage.stageHeight / 2) - dummy.height / 2;
			switch(posSrc) {				
				case"top left":
					dummy.x = left;
					dummy.y = top;				
					break;
				case"top center":
					dummy.x = centerWidth;
					dummy.y = top;				
					break;				
				case"top right":
					dummy.x = right;
					dummy.y = top;			
					break;
				case"center left":			
					dummy.x = left;
					dummy.y = centerHeight;
					break;		
				case"center right":
					dummy.x = right;
					dummy.y = centerHeight;				
					break;
				case"bottom left":
					dummy.x = left;
					dummy.y = bottom;			
					break;
				case"bottom center":
					dummy.x = centerWidth;
					dummy.y = bottom;
					break;
				case"bottom right":
					dummy.x = right;
					dummy.y = bottom;	
					break;
				default:
					dummy.x = centerWidth;
					dummy.y = centerHeight;
			}
		}
	}
}
